﻿using System;
using System.Collections.Generic;
using System.Data;
using DevelopTool.Bll.DbBlls;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model;
using DevelopTool.Model.DbModels;
using DevelopTool.My.Plugins.Bll;
using Sunny.UI;

namespace DevelopTool.My.Plugins.Forms
{
    /// <summary>
    /// 报表查看
    /// </summary>
    public partial class ReportViewForm : PluginsForm
    {
        /// <summary>
        /// 数据库连接信息
        /// </summary>
        private DbInfo dbInfo;

        /// <summary>
        /// DataGridView列信息集合
        /// </summary>
        private List<DataGridColumn> dataGridColumns = new List<DataGridColumn>()
              {
                { new DataGridColumn("ID"){ColumnText="报表ID",Width=0}},
                { new DataGridColumn("NAME"){ColumnText="报表名称",Width=0,ColumnType = ColumnType.DefaultFilter|ColumnType.RowMain|ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="NAME",TextType="Text",ShowTextLength=25
                }}},
                { new DataGridColumn("TYPE"){ColumnText="类别",Width=200}},
                { new DataGridColumn("SQL"){ColumnText="Sql内容",Width=200,ColumnType = ColumnType.DefaultFilter|ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="NAME"
                }}},
                { new DataGridColumn("XMLDATA"){ColumnText="Xml内容",Width=100,ColumnType = ColumnType.Hide|ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="NAME",TextType="XML"
                    }}},
                { new DataGridColumn("ISVALID"){ColumnText="是否有效",Width=-1}},
                { new DataGridColumn("VERSION"){ColumnText="版本号",Width=-1}},
              };

        public ReportViewForm()
        {
            InitializeComponent();
        }

        private void ReportViewForm_Load(object sender,EventArgs e)
        {

        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="dbInfo"></param>
        private void LoadDate(DbInfo dbInfo)
        {            
            try
            {
                DbManagerBll managerBll = new DbManagerBll(dbInfo);
                if(!managerBll.ExistTable(null,"QUICKREPORT_REPORTS"))
                {
                    this.ShowWarningDialog("此数据库中不存在报表主表：quickreport_reports");
                    return;
                }

                DataTable dataTable = new DataTable();

                LoadingHelper.ShowLoading("获取报表数据",() =>
                {                    
                    dataTable = new ReportViewBll(dbInfo).GetReports();//查询报表数据
                });

                this.dataDisplayControl.BindData(dataTable,this.dataGridColumns,"NAME ASC");
            }
            catch(Exception ex)
            {
                this.ShowErrorDialog(ex.Message);
            }            
        }


        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataDisplayControl_DataReLoad(object sender,EventArgs e)
        {
            LoadDate(this.dbInfo);
        }


        /// <summary>
        /// 选择数据库连接事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dbinfoComboBox_DbInfoSelected(object sender,DevelopTool.Forms.CommonControls.DbinfoEventArgs e)
        {
            if(e.IsConnect)
            {
                this.dbInfo = e.DbInfo;

                LoadDate(this.dbInfo);

            }
        }

    }
}
